<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\model\Banner;
use app\common\model\Config;
use app\common\services\UrlService;         // url
use app\common\services\CourseService;      // 课程


/**
 * 首页接口
 */
class Index extends Api
{
    protected $noNeedLogin = ['index','test'];
    protected $noNeedRight = ['*'];

    public function test()
    {
        $page = $this->request->post('page',1);
        $page_size = $this->request->post('limit',10);
        $result = \app\admin\model\exam\Question::page($page, $page_size)->select();
        foreach ($result as $v){
            if($v['kind'] == 'CALL'){
                if($v['answer']){
                    $content = json_decode($v['answer'],true);
                    foreach ($content['config'] as $k=>$vs){
                        if($s = strpos($vs['answer'],'FL')){
                            $vs['content'] = substr_replace($vs['content'],'FL',$s,0);
                            $content['config'][$k]['content'] = str_replace('FL','flight level',$vs['content']);
                        }
                    }
                    \app\admin\model\exam\Question::where('id',$v['id'])->update(['answer'=>json_encode($content)]);
                }
                continue;
            }
            if($v['xf_content']){
                if(strpos($v['xf_content'],'FL') !== false){
                    \app\admin\model\exam\Question::where('id',$v['id'])->update(['xf_content'=>str_replace('FL','flight level',$v['xf_content'])]);
                }

            }
        }
        $this->success('success',$result?1:0);
    }
    /**
     * 首页接口
     */
    public function index()
    {
        $result['banner']    = $this->getBanner();
        $result['recommend'] = $this->recommend($this->auth->id);
        $this->success('success',$result);
    }


    /**
     * 获取首页banner
     *
     * @return void
     */
    protected function getBanner(){

        $where['show_switch'] = 1;
        $where['pos'] = 'INDEX_TOP';
        $fields = "id,title,type,url,images";
        $result = Banner::where($where)
                ->field($fields)
                ->select();

        if(!empty($result)){
            foreach($result as &$value){

                //  url 路径拼接
                $value['url']    = UrlService::getFileUrl($value['url']);
                $value['images'] = UrlService::getFileUrl($value['images']);
                unset($value);
            }
        }
        return $result ?? [];
    }



    public function aboutus()
    {
        $data = Config::where('name','about_us')->value('value');
        return $this->success('success',$data);
    }


    /**
     * 推荐课程
     *
     * @param $user
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function recommend($user){

        $list = CourseService::getHotList($user);
        return $list;
    }
}
